package com.lee.shiro.service;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Component;

import com.lee.exception.UserPasswordNotMatchException;
import com.lee.system.entity.User;

/**
 * 登录密码方法
 * 
 * @author LiPengFei
 *
 */
@Component
public class PasswordService {


	public void validate(User user, String password) {

		if (!matches(user, password)) {
			throw new UserPasswordNotMatchException();
		}
	}

	public boolean matches(User user, String newPassword) {
		return user.getPassword().equals(encryptPassword(user.getLoginName(), newPassword, user.getSalt()));
	}

	public String encryptPassword(String username, String password, String salt) {
		return new Md5Hash(username + password + salt).toHex().toString();
	}
	
	public static void main(String[] args) {
	}
}
